package com.bbs.controller;

import com.bbs.service.adminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class adminController {
    @Autowired
    adminService admin;
    @RequestMapping("/login")
    public ModelAndView login(String name, String pwd){
        //获取用户对象
        Subject subject= SecurityUtils.getSubject();
        //获取用户账户令牌信息
        UsernamePasswordToken token=new UsernamePasswordToken(name,pwd);
        ModelAndView model=new ModelAndView();
        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            model.addObject("msg","用户不存在");
            model.setViewName("/login");
            return model;
        }catch (IncorrectCredentialsException e){
            model.addObject("msg","用户密码有误，请核实");
            model.setViewName("/login");
            return model;
        }
        model.addObject("name","欢迎您的到来:"+name);
        model.setViewName("/admin/viewAdmin");
        return model;
    }
    @RequestMapping("/test")
    public ModelAndView view(){
        ModelAndView view = new ModelAndView();
        Map<String,String> map=new LinkedHashMap<>();
        map.put("name","haha");
        view.addAllObjects(map);
        view.setViewName("/admin/viewAdmin");
        return view;
    }
    @RequestMapping("/noauthor")
    public String noAuthor(){
        return "未有相应权限，请核查";
    }
}
